package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.util.StringUtils;
import com.settlement.system.mapper.SysCompanyMapper;
import com.settlement.system.model.entity.SysCompany;
import com.settlement.system.model.form.CompanyForm;
import com.settlement.system.model.query.CompanyPageQuery;
import com.settlement.system.model.vo.SysCompanyImportVo;
import com.settlement.system.service.SysCompanyService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 菜单业务实现类
 *
 * @author haoxr
 * @since 2020/11/06
 */
@Service
@RequiredArgsConstructor
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements SysCompanyService {

    @Override
    public SysCompany getCompany(Long id) {
        return this.getById(id);
    }

    @Override
    public SysCompany getCompanyByCode(String code) {
        return this.getOne(new LambdaQueryWrapper<SysCompany>().eq(SysCompany::getCode, code));
    }

    @Override
    public Boolean saveCompany(CompanyForm company) {
        SysCompany sysCompany = new SysCompany();
        BeanUtils.copyProperties(company, sysCompany);
        if (company.getId() == null) {
            long count = this.count(new LambdaQueryWrapper<SysCompany>().eq(SysCompany::getName, company.getName()));
            Assert.isTrue(count == 0, "入账主体名称已存在");
        } else {
            long count = this.count(new LambdaQueryWrapper<SysCompany>().eq(SysCompany::getName, company.getName()).ne(SysCompany::getId, company.getId()));
            Assert.isTrue(count == 0, "入账主体名称已存在");
        }
        return this.saveOrUpdate(sysCompany);
    }

    @Override
    public boolean deleteCompanies(String idsStr) {
        Assert.isTrue(StringUtils.hasText(idsStr), "删除的公司数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public Page<SysCompany> getCompanyPage(CompanyPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        // 查询数据
        Page<SysCompany> companyPage = this.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<SysCompany>()
                .like(StringUtils.hasText(keywords), SysCompany::getCode, keywords)
                .like(StringUtils.hasText(queryParams.getName()), SysCompany::getName, queryParams.getName()));
        return companyPage;
    }

    @Override
    public List<SysCompanyImportVo> findByIds(String idsStr) {
        List<SysCompanyImportVo> voList = new ArrayList<>();
        if (!StringUtils.hasText(idsStr)) {
            return voList;
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).toList();
        List<SysCompany> companyList = this.getBaseMapper().selectBatchIds(ids);
        SysCompanyImportVo vo;
        for (SysCompany sysCompany : companyList) {
            vo = new SysCompanyImportVo();
            BeanUtils.copyProperties(sysCompany, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void importCompanies(List<SysCompanyImportVo> vos) {
        List<String> codeList = vos.stream().map(SysCompanyImportVo::getCode).filter(StringUtils::hasText).toList();
        List<SysCompany> sysCompanies = this.list(new LambdaQueryWrapper<SysCompany>().in(SysCompany::getCode, codeList));
        // 检查编码是否存在
        for (SysCompanyImportVo vo : vos) {
            Assert.isTrue(sysCompanies.stream().anyMatch(item -> Objects.equals(vo.getCode(), item.getCode())), "入账主体编码【%s】不存在".formatted(vo.getCode()));
        }
        SysCompanyImportVo importVo;
        for (SysCompany sysCompany : sysCompanies) {
            importVo = vos.stream().filter(item -> Objects.equals(item.getCode(), sysCompany.getCode())).findFirst().get();
            sysCompany.setName(importVo.getName());
            sysCompany.setFullName(importVo.getFullName());
            sysCompany.setBookCode(importVo.getBookCode());
            sysCompany.setBookName(importVo.getBookName());
            sysCompany.setFinanceCompanyCode(importVo.getFinanceCompanyCode());
        }
        this.updateBatchById(sysCompanies);
    }
}
